using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for TripleDes
/// </summary>
/// 
public class TripleDes
{
   public TripleDes() { }
   
   static readonly string Key = "Do8a0kbcRh8=";
   static readonly string IV = "Cg8abwQLDA0=";

   public static byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV)
   {
      try
      {
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
         cs.Write(Data, 0, Data.Length);
         cs.FlushFinalBlock();
         return ms.ToArray();
      }
      catch (Exception err)
      {
         return System.Text.Encoding.UTF8.GetBytes("ERROR: " + err.Message);
      }
   }
   public static byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV)
   {
      try
      {
         DESCryptoServiceProvider des = new DESCryptoServiceProvider();
         MemoryStream ms = new MemoryStream();
         CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(Key, IV), CryptoStreamMode.Write);
         cs.Write(Data, 0, Data.Length);
         cs.FlushFinalBlock();
         return ms.ToArray();
      }
      catch (Exception err)
      {
         return System.Text.Encoding.UTF8.GetBytes("ERROR: " + err.Message);
      }
   }

   public static string Encrypt_ToBase64(byte[] stringToEncrypt, string Key_Base64, string IV_Base64)
   {
      return Convert.ToBase64String(Encrypt(
         stringToEncrypt,
         Convert.FromBase64String(Key_Base64),
         Convert.FromBase64String(IV_Base64)));
   }

   public static string Encrypt_ToBase64(string stringToEncrypt, string Key_Base64, string IV_Base64, bool isUnicode)
   {
      return Convert.ToBase64String(Encrypt(
         (isUnicode ? System.Text.Encoding.Unicode.GetBytes(stringToEncrypt) :
         System.Text.Encoding.ASCII.GetBytes(stringToEncrypt)),
         Convert.FromBase64String(Key_Base64),
         Convert.FromBase64String(IV_Base64)));
   }

   public static string Encrypt_ToBase64(string stringToEncrypt, string Key_Base64, string IV_Base64)
   {
      return Encrypt_ToBase64(stringToEncrypt, Key_Base64, IV_Base64, true);
   }

   public static string Decrypt_FromBase64(string stringToDecrypt, string Key_Base64, string IV_Base64)
   {
      return Decrypt_FromBase64(stringToDecrypt, Key_Base64, IV_Base64, true);
   }
   public static string Decrypt_FromBase64(string stringToDecrypt, string Key_Base64, string IV_Base64, bool isUnicode)
   {
      byte[] result = Decrypt(
   Convert.FromBase64String(stringToDecrypt),
   Convert.FromBase64String(Key_Base64),
   Convert.FromBase64String(IV_Base64));

      return (isUnicode ? System.Text.Encoding.Unicode.GetString(result) :
         System.Text.Encoding.ASCII.GetString(result));
   }
   public static byte[] Decrypt_FromBase64_ToByteArray(string stringToDecrypt, string Key_Base64, string IV_Base64)
   {
      byte[] result = Decrypt(
   Convert.FromBase64String(stringToDecrypt),
   Convert.FromBase64String(Key_Base64),
   Convert.FromBase64String(IV_Base64));

      return result;
   }

   public static string Base64_Encrypt(string Data)
   {
      return Encrypt_ToBase64(Data, Key, IV);
   }

   public static string Base64_Decrypt(string Data)
   {
      return Decrypt_FromBase64(Data, Key, IV);
   }
}